import {
  ResType,
  UserInfo,
  LoginResType,
  AuthorDraft,
  CollectionFolder,
  AuthorInfo,
  Article,
  UploadAvatarResType,
} from "@/apis/shared.ts";
import requestInstance from "@/utils/requestInstance.ts";

/**
 * 登录接口
 * @param email 用户邮箱
 * @param password 用户密码
 * @return { code: number, message: string, data: { token: string }}
 */
export async function login({
  email,
  password,
}: {
  email: string;
  password: string;
}) {
  const res = await requestInstance.post<ResType<LoginResType>>(
    "/public/login",
    {
      email,
      password,
    }
  );

  return res.data;
}

/**
 * 注册接口
 * @param email 用户邮箱
 * @param password 用户密码
 * @return { code: number, message: string }
 */
export async function register({
  email,
  password,
}: {
  email: string;
  password: string;
}) {
  console.log("register at registerApi");
  const res = await requestInstance.post<ResType>("/public/register", {
    email,
    password,
  });
  return res.data;
}

/**
 * 获取用户详细信息
 */
export async function getUserInfo() {
  const res = await requestInstance.get<ResType<UserInfo>>(
    "/private/getAuthorDetailInfo"
  );

  return res.data;
}

/**
 * 获取作者草稿列表
 * @param
 * @return string[]
 */
export async function getAuthorDraftList() {
  const res = await requestInstance.get<ResType<AuthorDraft[]>>(
    "/private/getAuthorDraftList"
  );

  return res.data;
}

/**
 * 获取作者收藏列表
 * @param
 * @return
 */
export async function getAuthorCollectionList() {
  const res = await requestInstance.get<ResType<CollectionFolder[]>>(
    "/private/getCollectionFolders"
  );

  return res.data;
}

/**
 * 用户获取作者信息
 * @param email
 * @return
 */
export const userGetAuthorInfo = async (email: string) => {
  const res = await requestInstance.get<ResType<AuthorInfo<Article>>>(
    "/public/getAuthorInfo",
    {
      params: {
        authorId: email,
      },
    }
  );

  return res.data;
};

/**
 * 用户上传头像
 * @param file
 * @return
 */
export const userUploadAvatar = async (file: File) => {
  const res = await requestInstance.post<ResType<UploadAvatarResType>>(
    "/private/uploadAvatar",
    {
      file: file,
    },
    {
      headers: {
        "Content-Type": "multipart/form-data",
      },
    }
  );

  return res.data;
};

/**
 * 用户修改个人信息
 * @param {object} params - 包含昵称、描述和个人密码的对象
 * @param {string} params.nickname - 用户的新昵称
 * @param {string} params.description - 用户的新描述
 * @param {string} params.password - 用户的新密码
 * @return {Promise<ResType>} - 返回服务器响应的数据
 */
export const updateUserInfo = async ({
  nickname,
  description,
  password,
}: {
  nickname: string;
  description: string;
  password: string;
}) => {
  const res = await requestInstance.post<ResType>("/private/modifyUserInfo", {
    nickname,
    description,
    password,
  });

  return res.data;
};

/**
 * 获取作者文章列表
 * @param
 * @return string[]
 */
export async function getAuthorArticleList() {
  const res = await requestInstance.get<ResType<Article[]>>(
    "/private/getAuthorArticleList"
  );

  return res.data;
}

/**
 * 用户关注作者
 * @param
 * @return
 */
export const userFollowAuthor = async (authorEmail: string) => {
  const res = await requestInstance.get<ResType<string>>(
    "/private/subscribeAuthor",
    {
      params: {
        authorEmail,
      },
    }
  );

  return res.data;
};

/**
 * 用户取消作者
 * @param authorEmail
 * @return
 */
export const userUnFollowAuthor = async (authorEmail: string) => {
  const res = await requestInstance.get<ResType<string>>(
    "/private/unsubscribeAuthor",
    {
      params: {
        authorEmail,
      },
    }
  );

  return res.data;
};

/**
 * 用户点赞文章
 * @param
 * @return
 */
export const userLikeArticle = async (articleId: string) => {
  const res = await requestInstance.get<ResType<string>>("/private/likes", {
    params: {
      articleId,
    },
  });

  return res.data;
};

/**
 * 用户取消文章的点赞
 * @param
 * @return
 */
export const userUnLikeArticle = async (articleId: string) => {
  const res = await requestInstance.get<ResType<string>>(
    "/private/cancelLikes",
    {
      params: {
        articleId,
      },
    }
  );

  return res.data;
};

/**
 * 用户添加收藏夹
 * @param
 * @return
 */
export const userAddFolder = async (folderName: string) => {
  const res = await requestInstance.post<ResType>(
    "/private/addCollectionFolder",
    {
      folderName,
    }
  );

  return res.data;
};

/**
 * 用户删除文件夹
 * @param
 * @return
 */
export const userDeleteFolder = async (folderName: string) => {
  const res = await requestInstance.delete<ResType<string>>(
    "/private/deleteCollectionFolder",
    {
      params: {
        folderName,
      },
    }
  );

  return res.data;
};

/**
 * 用户添加文章到收藏夹中
 * @param
 * @return
 */
export const userAddFolderArticle = async (
  folderName: string,
  articleId: string
) => {
  const res = await requestInstance.post<ResType>(
    "/private/addArticleToCollectionFolder",
    {
      folderName,
      articleId,
    }
  );

  return res.data;
};

/**
 * 用户删除收藏夹中的某个文章
 * @param
 * @return
 */
export const userDeleteFolderArticle = async (
  folderName: string,
  articleId: string
) => {
  const res = await requestInstance.delete<ResType<string>>(
    "/private/deleteArticleFromCollectionFolder",
    {
      params: {
        folderName,
        articleId,
      },
    }
  );

  return res.data;
};
